package com.duzhuo.springsecurity.config;

import com.duzhuo.springsecurity.utils.SecurityUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class UserDetailsServiceImp implements UserDetailsService {
    @Autowired
    private PasswordEncoder encoder;

    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        //TODO 查询数据库封装 SecurityUser
        String encode = encoder.encode("123");
        SecurityUser securityUser = new SecurityUser();
        securityUser.setUsername(userName);
        securityUser.setPassword(encode);
        ArrayList<String> strings = new ArrayList<>();
        strings.add("admin");
        strings.add("vip1");
        strings.add("vip2");
        securityUser.setPermissionValueList(strings);
        return securityUser;

//        return new User(userName, encode, AuthorityUtils.commaSeparatedStringToAuthorityList("admin,vip1,vip2"));
    }
}
